/**
    ATClab - scriptable Air Traffic Control simulations.
    Copyright (C) 2010 openatclab@gmail.com

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
**/

#ifndef __ATC_CANVASVIEW_HDR__ 
#define __ATC_CANVASVIEW_HDR__


#include <q3canvas.h>
//Added by qt3to4:
#include <QResizeEvent>
#include <QShowEvent>
#include <QWheelEvent>
#include <QEvent>
#include <QMouseEvent>
#include <QKeyEvent>

#include "atc_types.h"
#include "canvas_action.h"
#include "canvas_types.h"
#include "param_impl.h"

#include <map>
#include <memory>


namespace atc{

	/*!
	 *
	 */
	class ACAltBlk;
	class ACVelBlk;
	class ACLocator;
	class ACTRouteLine;
	class AircraftAgent;
	class BRLDataSprite;
	class Canvas;
	class CFLSelector;
	class EfficiencyQuestion;
	class HeadingSelector;
	class ListBox;
	class RequirementSelector;
	class VelocitySelector;
	class WorkloadQuestion;
	class WPLocator;


	/*!
	 *
	 */
	typedef std::map<CanvasItem::TYPE,CanvasEventHandler*>	HandlerMap;
	typedef std::map<CanvasItem::TOOL,CanvasEventHandler*>	ToolHandlerMap;


	/*!
	 *
	 */
	class CanvasView : public Q3CanvasView {
		Q_OBJECT

	friend class CanvasEventHandler;

	public:
		CanvasView( Q3Canvas *, QWidget *, const char * );
		~CanvasView();

	public:
		Canvas * atc_canvas() const;

		void record_event( KeyValues & );

//		void select_aircraft	( ACLocator *     );
//		void select_altitude	( ACAltBlk *      );
//		void select_brl			( BRLDataSprite * ); 
//		void select_velocity	( ACVelBlk *      );
//		void select_waypoint	( WPLocator *     );

//		void select_scale		();

		void set_select_handler	( CanvasItem::TYPE, CanvasEventHandler* );
		void set_action_handler	( CanvasItem::TYPE, CanvasEventHandler* );
		void set_tool_handler	( CanvasItem::TOOL, CanvasEventHandler* );

		void set_keyed_tools( const KeyToolMap &, const KeyTagMap & );

		Q3CanvasItem* find_item( const QPoint & );

		CanvasItem::TOOL tool() { return _tool; }
		void clear_tool() { _tool = CanvasItem::NO_TOOL; }

		void pause() { emit( signal_pause() ); }
		void resume() { emit( signal_resume() ); }

	public slots:
		void slot_paused() { _paused = true; }
		void slot_resumed() { _paused = false; }

	private:
		bool loadToolHandler( CanvasItem::TOOL );
		bool loadEventHandler( QEvent * );
		bool _loadEventHandler( const QPoint &, HandlerMap & );
		void unloadEventHandler( CanvasEventHandler * );


	protected: //virtual event handlers
		void showEvent                    ( QShowEvent *   ) { setFocus(); }
		void contentsMousePressEvent      ( QMouseEvent *e ) { actionEvent( e ); }
		void contentsMouseReleaseEvent    ( QMouseEvent *e ) { actionEvent( e ); }
		void contentsMouseDoubleClickEvent( QMouseEvent *e ) { actionEvent( e ); }
		void contentsMouseMoveEvent       ( QMouseEvent *e ) { if ( _pointer ) actionEvent( e ); }
		void contentsWheelEvent           ( QWheelEvent *  ) {};

		void keyPressEvent                ( QKeyEvent *    );
		void resizeEvent                  ( QResizeEvent * );

	private:
		void actionEvent( QEvent * );
		void set_tool_key( int );

		//void record_event( KeyValues & );

	signals:
		void event_recorded( KeyValues & );

		void signal_pause();
		void signal_resume();

	private:
		HandlerMap		_selectHandlers;
		HandlerMap		_actionHandlers;
		ToolHandlerMap	_toolHandlers;

		KeyToolMap _keyTools;
		KeyTagMap  _keyTags;

		CanvasEventHandler*	_eventHandler;

		CanvasItem::TOOL _tool;

		int    _key;
		bool   _pointer;

		bool   _paused;
	};


};


#endif
